package com.xccwk.serviceImp;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xccwk.DAO.UserDao;
import com.xccwk.model.User;
import com.xccwk.service.UserService;

public class UserServiceImp implements UserService{


	List<User> listUser=new ArrayList<User>();
		public List<User> findUserById(String sql,int id) {
			listUser.clear();
			try {
				PreparedStatement ps=UserDao.
						getConnection().prepareStatement(sql);
				ps.setLong(1, id);
				ResultSet rs=ps.executeQuery();
				int i=0;
				System.out.println(rs.toString());
				while(rs.next()) {
					User user=new User();
					user.setId(Long.parseLong(rs.getString(1)));
					user.setUserName(rs.getString(2));
					user.setUserPassword(rs.getString(3));
					i++;
					listUser.add(user);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return listUser;
		}

		public int insert(String sql,User user) {
			int result = 0;
			PreparedStatement ps;
			try {
				ps = UserDao.getConnection().prepareStatement(sql);
				ps.setLong(1, user.getId());
				ps.setString(2, user.getUserName());
				ps.setString(3,user.getUserPassword());
				result=ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}

		
		public int delete(String sql,String id) {
			int result = 0;
			PreparedStatement ps;
			System.out.println(sql);
			try {
				ps = UserDao.getConnection().prepareStatement(sql);
				ps.setString(1, id);
				result=ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}
		
		public List<User> avageSelect(String sql,String like){
			listUser.clear();
			System.out.println(sql);
			try {
				PreparedStatement ps=UserDao.
						getConnection().prepareStatement(sql);
				ps.setString(1,"%"+like+"%");
				ResultSet rs=ps.executeQuery();
				int i=0;
				while(rs.next()) {
					User user=new User();
					ps.setLong(1, user.getId());
					ps.setString(2, user.getUserName());
					ps.setString(3,user.getUserPassword());
					i++;
					listUser.add(user);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(listUser);
			return listUser;		
		}

		public List<User> findAll(String sql) {
			listUser.clear();
			try {
				PreparedStatement ps=UserDao.
						getConnection().prepareStatement(sql);
				ResultSet rs=ps.executeQuery();
				int i=0;
				while(rs.next()) {
					User user=new User();
					ps.setLong(1, user.getId());
					ps.setString(2, user.getUserName());
					ps.setString(3,user.getUserPassword());
					i++;
					listUser.add(user);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return listUser;
		}
		
		
		public List<String> selectById(String sql){
			List<String> listInt=new ArrayList<String>();
			try {
				PreparedStatement ps=UserDao.
						getConnection().prepareStatement(sql);
				ResultSet rs=ps.executeQuery();
				int i=0;
				while(rs.next()) {
					listInt.add(rs.getString(1));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return listInt;
		}
		
		
		
		

}
